/******************************************************************************

                          MAIN SOURCE FILE

 *******************************************************************************/
/**********************************************************************
 * ? 2010 Accel Transmatic Ltd.
 *
 * FileName:        MAIN.c
 * Dependencies:    Header (.h) files if applicable, see below
 * Processor:       PIC32
 * Compiler:        MPLAB? C32
 *
 * SOFTWARE LICENSE AGREEMENT:
 * Accel Transmatic retains all ownership and intellectual property rights in
 * the code accompanying this message and in all derivatives hereto.
 ************************************************************************/
#define DEBUG_ENABLED
#include <p32xxxx.h>
#include <peripheral\system.h>
#include "GenericTypeDefs.h"
#include "uart.h"
#include "Timer.h"
#include "ISO_ME.h"


#define SYS_FREQ 				(80000000L)
// *****************************************************************************
// *****************************************************************************
// Configuration Bits
// *****************************************************************************
// *****************************************************************************

#ifndef OVERRIDE_CONFIG_BITS

    #pragma config UPLLEN   = ON            // USB PLL Enabled
    #pragma config FPLLMUL  = MUL_20        // PLL Multiplier
    #pragma config UPLLIDIV = DIV_2         // USB PLL Input Divider
    #pragma config FPLLIDIV = DIV_2         // PLL Input Divider
    #pragma config FPLLODIV = DIV_1         // PLL Output Divider
    #pragma config FPBDIV   = DIV_1         // Peripheral Clock divisor
    #pragma config FWDTEN   = OFF           // Watchdog Timer
    #pragma config WDTPS    = PS1           // Watchdog Timer Postscale
    #pragma config FCKSM    = CSDCMD        // Clock Switching & Fail Safe Clock Monitor
    #pragma config OSCIOFNC = OFF           // CLKO Enable
    #pragma config POSCMOD  = HS            // Primary Oscillator
    #pragma config IESO     = OFF           // Internal/External Switch-over
    #pragma config FSOSCEN  = OFF           // Secondary Oscillator Enable
    #pragma config FNOSC    = PRIPLL        // Oscillator Selection
    #pragma config CP       = OFF           // Code Protect
    #pragma config BWP      = OFF           // Boot Flash Write Protect
    #pragma config PWP      = OFF           // Program Flash Write Protect
    #pragma config ICESEL   = ICS_PGx2      // ICE/ICD Comm Channel Select
    #pragma config DEBUG    = OFF           // Debugger Disabled for Starter Kit

#endif // OVERRIDE_CONFIG_BITS



void main(void)
{
    int  value;
    BYTE i = 0,j = 0;
    BYTE connect=0;
    BYTE index;
    BYTE ret;

    // value = SYSTEMConfigWaitStatesAndPB( GetSystemClock() );

    /* Configure system for maximum performance
     * and enable multi vector interrupts. */

    SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
    INTEnableInterrupts();

    value = OSCCON;
    while (!(value & 0x00000020))
    {
        value = OSCCON;    // Wait for PLL lock to stabilize
    }

    // Enable the cache for the best performance
    CheKseg0CacheOn();
    InitUart(UART_2);

    init_timer(TIMER1); // Used for delay(timeout)
    init_timer(TIMER2); // Used for hard delay
    init_timer(TIMER4); // Used for LED event functions

    connect = check_iso();
    while(1)
    {
        if(byte_received)
        {
            byte_received = FALSE;
            index = data_received;
            if((connect == ISO_9141_5BAUD_CONNECT)||(connect == KWP_SLOW_CONNECT))
            {
                ret = iso_send(index);
            }
            else if(connect == KWP_FAST_CONNECT)
            {
                
            }
        }        
    }

}

